package com.tuo51.manager.controller;


import com.tuo51.evaluate.domain.appusers.PromoCode;
import com.tuo51.core.repositories.StaffRepository;
import com.tuo51.evaluate.repositories.appusers.PromoCodeRepository;
import com.tuo51.core.service.StaffService;
import com.tuo51.evaluate.vo.PromoCodeVO;
import com.tuo51.foudation.exception.BusinessException;
import com.tuo51.foudation.model.ErrorCode;
import com.tuo51.foudation.model.MemVerificationCache;
import com.tuo51.foudation.model.StatusResponse;
import com.tuo51.foudation.utils.CopyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import static com.tuo51.foudation.constant.RequestConstant.TICKET;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

@RestController
@RequestMapping("/mapi/promocode")
@Api(value = "/mapi/promocode", tags = "OP - 优惠码")
public class MAPIPromoCodeController {

    @Autowired
    PromoCodeRepository codeRepository;

    @Autowired
    StaffService staffService;

    @Autowired
    StaffRepository staffRepository;

    @Autowired
    MemVerificationCache cache;

    @RequestMapping(value = "/list", method = GET)
    @ApiOperation(value = "资源列表")
    public StatusResponse list(@RequestHeader(TICKET) String ticket,
                                   @RequestParam(value = "pageNum", required = false, defaultValue = "0")Integer pageNum,
                                   @RequestParam(value = "pageSize", required = false, defaultValue = "15")Integer pageSize,
                                   @RequestParam(value = "productId", required = false) Long productId){

        cache.getStaffId(ticket);

        PageRequest pageRequest = PageRequest.of(pageNum, pageSize);

        Page<PromoCode> ret = codeRepository.findAll(new Specification<PromoCode>() {
            @Override
            public Predicate toPredicate(Root<PromoCode> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

                Predicate base = cb.isFalse(root.get("deleted"));

                if (productId != null){
                    Predicate p = cb.equal(root.get("product_id"), productId);
                    base = cb.and(base, p);
                }

                query.where(base);

                query.orderBy(cb.desc(root.get("salesQuantity")), cb.desc(root.get("created")));

                return query.getRestriction();
            }
        }, pageRequest);

        return StatusResponse.success(ret);
    }

    @RequestMapping(value = "/info", method = GET)
    @ApiOperation(value = "内容详情")
    public StatusResponse info(@RequestHeader(TICKET) String ticket,
                                   @RequestParam(value = "codeId") String codeId){

        cache.getStaffId(ticket);

        PromoCode ret = codeRepository.getOne(codeId);

        if (ret == null){
            throw new BusinessException(ErrorCode.MATERIAL_NOT_EXIST, "条目不存在");
        }

        return StatusResponse.success(ret);
    }

    @RequestMapping(value = "/edit", method = POST)
    @ApiOperation(value = "优惠码详情编辑")
    public StatusResponse edit(@RequestHeader(TICKET) String ticket,
                                    PromoCodeVO promoCode){
        cache.getStaffId(ticket);

        PromoCode ret = null;
        if (promoCode.getId() != null){
            ret = codeRepository.getOne(promoCode.getId());
        }else{
            ret = new PromoCode();
        }
        CopyUtils.copyProperties(promoCode, ret);

        codeRepository.save(ret);

        return StatusResponse.success(ret);
    }

    @RequestMapping(value = "/delete", method = POST)
    @ApiOperation(value = "删除")
    public StatusResponse delete(@RequestHeader(TICKET) String ticket,
                                    String id){
        cache.getStaffId(ticket);



        if (StringUtils.isEmpty(id)){
            throw new BusinessException(ErrorCode.PARAM_NOT_BLANK, "参数错误");
        }

        PromoCode item = codeRepository.getOne(id);
        if (item == null){
            throw new BusinessException(ErrorCode.MATERIAL_NOT_EXIST, "对象不存在");
        }

        item.setDeleted(true);
        codeRepository.save(item);

        return StatusResponse.success(true);
    }
}
